class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


class Solution:
    @staticmethod
    def dp(root: TreeNode) -> tuple[int, int]:
        if root is None:
            return 0, 0     # l: 偷，r：不偷
        leftNode = Solution.dp(root.left)
        rightNode = Solution.dp(root.right)
        return root.val + leftNode[1] + rightNode[1], max(leftNode) + max(rightNode)

    @staticmethod
    def rob(root: TreeNode) -> int:
        if root is None:
            return 0
        return max(Solution.dp(root))
